<?php echo $header; ?>
<?php
$dir_root = explode("admin", DIR_APPLICATION);
?>
<link rel="stylesheet" type="text/css" href="view/javascript/jquery/uploadify/uploadify.css" />
<script type="text/javascript" src="view/javascript/jquery/uploadify/swfobject.js"></script>
<script type="text/javascript" src="view/javascript/jquery/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript">
// <![CDATA[
$(document).ready(function() {
  $(".uploadaj_msg span a").live('click', function(){
    $("#file_uploadUploader").css({"width":"104px", "height" : "27px"});
    $(".uploadaj_msg").html("");  
    $("#file_ajax").val("");
  });  
  $('#file_upload').uploadify({
    'uploader'  : 'view/javascript/jquery/uploadify/uploadify.swf',
    //'script'    : '<?php echo 'index.php?route=tool/export/upload_excel&token='.$this->session->data['token'];?>',
    'script'    : 'view/javascript/jquery/uploadify/uploadify.php',   
    'buttonImg' : 'view/javascript/jquery/uploadify/button.png', 
    'cancelImg' : 'view/javascript/jquery/uploadify/cancel.png',
    'folder'    : '<?php echo $dir_root[0]?>import',
    'auto'      : true,
    'width'     : 104,
    'height'    : 27,    
    'multi'     : false,
    'fileExt'   : '*.xls;',
    'fileDesc'  : 'File excel 2003 *.xls',    
    'onComplete': function(event, ID, fileObj, response, data) {
        json = $.parseJSON(response);
        if(json.error == '1'){
            alert(json.msg);
        }else{
            $("#file_ajax").val(json.msg); 
            $(".uploadaj_msg").html(json.msg + " <span><a>Remove</a></span>");   
        }
    },
    'onSelectOnce' : function(event,data) {
        $("#file_uploadUploader").css({"width":"0", "height" : "0"});
    },
    'onCancel'    : function(event,ID,fileObj,data) {
        $("#file_uploadUploader").css({"width":"104px", "height" : "27px"});
        $(".uploadaj_msg").html("");          
    },
    'onSWFReady'  : function() {
        $(".uploadaj_msg").html("");    
    }
  });
});
// ]]>
</script>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/backup.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="if(!$('#is_upload').is(':checked')){if(!$('#file_ajax').val()){alert('Please upload file!'); return false;}};$('#form').submit();" class="button"><span><?php echo $button_import; ?></span></a><a onclick="location='<?php echo $export; ?>'" class="button"><span><?php echo $button_export; ?></span></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <table class="form">
          <tr>
            <td colspan="2"><?php echo $entry_description; ?></td>
          </tr>
          <tr>
            <td width="25%">Local file?</td>
            <td><input type="checkbox" name="is_upload" id="is_upload" value="1"/></td>
          </tr>          
          <tr id="upfile">
            <td width="25%"><?php echo $entry_restore; ?></td>
            <td>
                <!--<input type="file" name="upload" />-->
                <input id="file_upload" type="file" name="file_upload" />
                <input type="hidden" name="file_ajax" id="file_ajax"/>
                <div class="uploadaj_msg">Loading...</div>
            </td>
          </tr>
          <tr id="localfile" style="display: none;">
            <td width="25%">Local file</td>
            <td>
                <select name="localfile" class="sel-localfile">
                    <?php foreach($local_excel as $file):?>
                    <option value="<?php echo $file?>"><?php echo $file?></option>
                    <?php endforeach;?>
                </select>
            </td>
          </tr>          
          <tr>
            <td>Action</td>
            <td>
                <select name="action">   
                    <option value="reset">Reset</option>
                    <option value="add">Add</option>
                </select>
            </td>
          </tr>
          <tr>
            <td>By Rows?</td>
            <td><input type="checkbox" name="byrow" id="byrow" value=""/></td>
          </tr>
          <tr style="display: none;" id="from_id">
            <td>
                From row ID
            </td>
            <td>
                <input type="text" name="from_row_id"/>
            </td>
          </tr>
          <tr style="display: none;" id="to_id">
            <td>
                To row ID
            </td>
            <td>
                <input type="text" name="to_row_id"/>
            </td>            
          </tr>          
        </table>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
    $("#byrow").click(function(){
        if($(this).is(":checked")){
            $("#from_id").show();  
            $("#to_id").show();  
        }else{
            $("#from_id").hide();  
            $("#to_id").hide();              
        }  
    });   
    $("#is_upload").click(function(){
        if($(this).is(":checked")){
            $(this).attr("disabled", true);
            $.ajax({
                type: "POST",
                url: '<?php echo 'index.php?route=tool/export/getDirExcelAjax&token='.$this->session->data['token'];?>',
                success: function(data){
                    json = $.parseJSON(data);
                    html = '';
                    console.log(data);
                    $.each(json.data, function(key, value){
                        html += '<option value="'+value+'">'+value+'</option>';    
                    });
                    $("#is_upload").removeAttr("disabled", false);
                    $(".sel-localfile").html(html);
                    $("#localfile").show();  
                    $("#upfile").hide();                      
                }    
            });
        }else{
            $("#localfile").hide();  
            $("#upfile").show();               
        }         
    }); 
});
</script>
<?php echo $footer; ?>